package com.ywxx.dyhelper.widget;


import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ywxx.dyhelper.R;

/**
 * 作用：自定义数字加减控件
 */
public class NumberAddSubView extends LinearLayout implements View.OnClickListener
{

    private TextView btn_sub;
    private TextView btn_add;
    private TextView tv_num;
    private Context mContext;

    /**
     * 设置默认值
     */
    private int value = 1;
    private int minValue = 1;
    private int maxValue = 5;
    private int seeknum = 1;

    public NumberAddSubView(Context context)
    {
        this(context, null);
    }

    public NumberAddSubView(Context context, AttributeSet attrs)
    {
        this(context, attrs, 0);
    }

    public NumberAddSubView(Context context, AttributeSet attrs, int defStyleAttr)
    {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initView(context);
    }

    private void initView(Context context)
    {
        //第三个参数：把当前View加载到NumberAddSubView控件上
        View.inflate(context, R.layout.number_add_sub_view, this);
        btn_sub = findViewById(R.id.btn_sub);
        btn_add = findViewById(R.id.btn_add);
        tv_num = findViewById(R.id.tv_num);

        btn_sub.setOnClickListener(this);
        btn_add.setOnClickListener(this);
    }

    public int getValue()
    {
        String val = tv_num.getText().toString();
        if (!TextUtils.isEmpty(val))
        {
            value = Integer.parseInt(val);
        }
        return value;
    }

    public void setValue(int value)
    {
        this.value = value;
        tv_num.setText(value + "");
    }

    public int getMinValue()
    {
        return minValue;
    }

    public void setSeekNum(int seeknum)
    {
        this.seeknum = seeknum;
    }

    public void setMinValue(int minValue)
    {
        this.minValue = minValue;
    }

    public int getMaxValue()
    {
        return maxValue;
    }

    public void setMaxValue(int maxValue)
    {
        this.maxValue = maxValue;
    }


    @Override
    public void onClick(View v)
    {
        if (v.getId() == R.id.btn_sub)
        {
//            Toast.makeText(mContext,"减",Toast.LENGTH_SHORT).show();

            subNum();
            if (onButtonClickListenter != null)
            {
                onButtonClickListenter.onButtonSubClick(v, value);
            }
        } else if (v.getId() == R.id.btn_add)
        {
//            Toast.makeText(mContext,"加",Toast.LENGTH_SHORT).show();
            addNum();
            if (onButtonClickListenter != null)
            {
                onButtonClickListenter.onButtonAddClick(v, value);
            }
        }
    }

    /**
     * 减少数据
     */
    private void subNum()
    {
        if (value > minValue)
        {
            value = value - seeknum;
            tv_num.setText(value + "");
        }
    }

    /**
     * 添加数据
     */
    private void addNum()
    {
        if (value < maxValue)
        {
            value = value + seeknum;
            tv_num.setText(value + "");
        }
    }

    public interface OnButtonClickListenter
    {
        /**
         * 当增加按钮被点击的时候回调该方法
         * @param view
         * @param value
         */
        public void onButtonAddClick(View view, int value);

        /**
         * 当减少按钮被点击的时候回调这个方法
         * @param view
         * @param value
         */
        public void onButtonSubClick(View view, int value);
    }

    private OnButtonClickListenter onButtonClickListenter;

    public void setOnButtonClickListenter(OnButtonClickListenter onButtonClickListenter)
    {
        this.onButtonClickListenter = onButtonClickListenter;
    }

}
